<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button message="后盾人">button</button>
    <button message="hdcms">button</button>
  </body>

  <script>
    // 浅拷贝的实现方法
    // function shallowCopy(array) {
    //   let newObj = array instanceof Array ? [] : {};
    //   for (let k in array) {
    //     newObj[k] = array[k];
    //   }
    //   return newObj;
    // }
    // let obj = {
    //   name: 'zs',
    //   data: [1, 2, 3],
    // };
    // let newObj = shallowCopy(obj);
    // newObj.name = 'cyl';
    // newObj.data[0] = 4;
    // console.log(newObj);
    // console.log(obj);

    // 深拷贝的用法
    function deepCopy(obj) {
      let newObj = obj instanceof Array ? [] : {};
      for (let [k, v] of Object.entries(obj)) {
        newObj[k] = typeof obj[k] == 'object' ? deepCopy(v) : v;
      }
      return newObj;
    }

    let obj = {
      name: 'zs',
      data: [1, 2, 3, 4],
    };
    let newObj = deepCopy(obj);
    newObj.name = 'cyl';
    newObj.data[0] = 4;
    console.log(newObj);
    console.log(obj);
  </script>
</html>
